import paramiko
import time

def try_connect_ssh(hostname, port, username, password):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    start_time = time.time()
    print("start connect qemu by ssh")
    
    while True:
        try:
            ssh.connect(hostname=hostname, port=port, username=username, password=password)
            end_time = time.time() - start_time
            break
        except:
            end_time = time.time() - start_time
            minutes = int(end_time // 60)
            seconds = int(end_time % 60)
            print("[time {}:{}] can not to connect qemu".format(minutes, seconds))
            time.sleep(1)
    return ssh, end_time


